package com.haier.uos.springcloud.basic.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.haier.uos.springcloud.basic.service.ICityDataService;
import com.haier.uos.springcloud.basic.service.IWeatherDataService;
import com.haier.uos.springcloud.basic.vo.City;
import com.haier.uos.springcloud.basic.vo.WeatherResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class WeatherDataServiceImpl implements IWeatherDataService {
    private Logger logger = LoggerFactory.getLogger(getClass());
    private static final String BASIC_URI = "http://wthrcdn.etouch.cn/weather_mini";
    private static final long TIME_OUT = 1800L;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public WeatherResponse getDataByCityId(String cityId) {
//        http://wthrcdn.etouch.cn/weather_mini?citykey=101280601
        String uri = BASIC_URI + "?citykey=" + cityId;

        return getWeatherData(uri);
    }


    @Override
    public WeatherResponse getDataByCityName(String cityName) {
        //http://wthrcdn.etouch.cn/weather_mini?city=西安
        String uri = BASIC_URI + "?city=" + cityName;
        return getWeatherData(uri);
    }

    @Override
    public void syncWeatherDataByCityId(String cityId) {
//        String uri = BASIC_URI + "?citykey=" + cityId;
//        saveWeatherDataToRedis(uri);
        getDataByCityId(cityId);
    }

    private void saveWeatherDataToRedis(String uri) {
        String data = null;
        ResponseEntity<String> forEntity = restTemplate.getForEntity(uri, String.class);
        if (forEntity.getStatusCodeValue() == 200) {
            data = forEntity.getBody();
            stringRedisTemplate.opsForValue().set(uri, data, TIME_OUT, TimeUnit.SECONDS);
        }
    }

    private WeatherResponse getWeatherData(String uri) {
        WeatherResponse weatherResponse = null;
        String data = null;
        ObjectMapper objectMapper = new ObjectMapper();
        if (stringRedisTemplate.hasKey(uri)) {
            logger.info("get by redis uri is {}", uri );
            data = stringRedisTemplate.opsForValue().get(uri);
        } else {
            logger.info("get by service uri is {}", uri);
            ResponseEntity<String> forEntity = restTemplate.getForEntity(uri, String.class);
            if (forEntity.getStatusCodeValue() == 200) {
                data = forEntity.getBody();
                stringRedisTemplate.opsForValue().set(uri, data, TIME_OUT, TimeUnit.SECONDS);
            }
        }
        try {
            weatherResponse = objectMapper.readValue(data, WeatherResponse.class);
        } catch (IOException e) {
            logger.error("objectMapper.readValue error", e);
            stringRedisTemplate.delete(uri);
        }
        return weatherResponse;
    }
}
